package com.wu.dao.impl;

import com.wu.bean.User;
import com.wu.dao.UserDao;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public User queryUserByUserName(String username) {
        String sql="SELECT * FROM USER WHERE username=?";
        return queryForOne(User.class,sql,username);
    }

    @Override
    public int saveUser(User user) {
        String sql="INSERT INTO `blogwu`.`user` (`username`, `password`,email) VALUES (?, ?,?)";
        return update(sql,user.getUsername(),user.getPassword(),user.getEmail());
    }

    @Override
    public User queryUsernameAndPassword(String username, String password) {
        String sql="SELECT * FROM USER WHERE username=? AND PASSWORD=?";
        return queryForOne(User.class,sql,username,password);
    }

    @Override
    public List<User> queryUserAll() {
        String sql="SELECT user.`id`,user.`username`,user.name,user.`sex`,user.`email`,user.`address`,user.`occupation`,user.`telephone` FROM USER";
        return queryForList(User.class,sql);
    }

    @Override
    public List<User> queryUserNameLike(String username) {
        String sql="SELECT user.`id`,user.`username`,user.name,user.`sex`,user.`email`,user.`address`,user.`occupation`,user.`telephone` FROM USER WHERE user.`username` LIKE ?";
        return queryForList(User.class,sql,"%"+username+"%");
    }

    @Override
    public int DeleteUser(int id) {
        String sql="DELETE FROM USER WHERE id=?";
        return update(sql,id);
    }

    @Override
    public int UpdateUserPassword(String username, String password) {
        String sql="UPDATE USER SET PASSWORD=? WHERE username=?";
        return update(sql,password,username);
    }

    @Override
    public int UpdateUser(User user) {
        String sql="UPDATE USER SET NAME=?,sex=?,email=?,address=?,occupation=?,telephone=? WHERE id=?";
        return update(sql,user.getName(),user.getSex(),user.getEmail(),user.getAddress(),user.getOccupation(),user.getTelephone(),user.getId());
    }
}
